package com.easychat.controller.Admin;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.controller.ABaseController;
import com.easychat.entity.enums.UserStatusEnum;
import com.easychat.entity.po.GroupInfo;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.query.GroupInfoQuery;
import com.easychat.entity.query.UserInfoQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.service.GroupInfoService;
import com.easychat.service.UserInfoBeautyService;
import com.easychat.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Author Dongdong
 * @注释 用户联系人
 */
@RestController("adminGroupInfoController")
@RequestMapping("/admin")
public class AdminGroupInfoController extends ABaseController {

    @Resource
    private GroupInfoService groupInfoService;

    @RequestMapping("/loadGroup")
    @GlobalInterceptor
    public ResponseVO loadGroup(GroupInfoQuery groupInfoQuery) {
        groupInfoQuery.setOrderBy("create_time desc");
        /*
        SELECT
        g.*,
        ( SELECT count( 1 ) FROM user_contact c WHERE c.contact_id = g.group_id ) AS memberCount ,
        ( SELECT u.nick_name FROM user_info u WHERE u.user_id = g.group_owner_id ) AS groupOwnerName
        FROM
        group_info g
         */
        groupInfoQuery.setQueryGroupOwnerNickName(true);
        groupInfoQuery.setQueryMemberCount(true);
        PaginationResultVO<GroupInfo> resultVo = groupInfoService.findListByPage(groupInfoQuery);
        return getSuccessResponseVO(resultVo);
    }

    @RequestMapping("/dissolutionGroup")
    @GlobalInterceptor
    public ResponseVO dissolutionGroup(@NotEmpty String groupId) {
        //分析：解散群的话，user_contact表中也需要删除，group info表中也要删除记录。

        GroupInfo groupInfo = groupInfoService.getGroupInfoByGroupId(groupId);
        groupInfoService.dissolutionGroup(groupInfo.getGroupOwnerId(),groupId);
        return getSuccessResponseVO("解散成功");

    }
}
